
import { resolveResError } from './helpers'
import { useAuthStore } from '@/store'
import {message} from "ant-design-vue";

export function setupInterceptors(axiosInstance) {

  function reqResolve(config) {
    console.log(config)

    // 处理不需要token的请求
    if (config.noNeedToken) {
      return config
    }

    const { accessToken } = useAuthStore()
    if (accessToken) {
      // token: Bearer + xxx
      config.headers.Authorization = 'Bearer ' + accessToken
    }
    config.headers.clientid = import.meta.env.VITE_APP_CLIENT_ID
    config.headers['Accept-Language'] = localStorage.getItem('language')

    return config
  }

  function reqReject(error) {
    return Promise.reject(error)
  }

  const SUCCESS_CODES = [0, 200]
  function resResolve(response) {
    const { data, status, config, statusText, headers } = response
    if (headers['content-type']?.includes('json')) {
      if (SUCCESS_CODES.includes(data?.code)) {
        return Promise.resolve(data.data)
      }
      const code = data?.code ?? status

      // 根据code处理对应的操作，并返回处理后的message
      const msg = resolveResError(code, data?.message ?? statusText)
      //需要错误提醒
      !config?.noNeedTip && msg && message?.error(msg)
      return Promise.reject({ code, msg, error: data ?? response })
    }
    return Promise.resolve(data ?? response)
  }

  async function resReject(error) {
    if (!error || !error.response) {
      const code = error?.code
      console.log(error)
      /** 根据code处理对应的操作，并返回处理后的message */
      const msg = resolveResError(code, error.message)
      message?.error(msg)
      return Promise.reject({ code, msg, error })
    }

    const { data, status, config } = error.response
    const code = data?.code ?? status

    const msg = resolveResError(code, data?.msg ?? error.message)
    /** 需要错误提醒 */
    !config?.noNeedTip && msg && message?.error(msg)
    return Promise.reject({ code, msg, error: error.response?.data || error.response })
  }

  axiosInstance.interceptors.request.use(reqResolve, reqReject)
  axiosInstance.interceptors.response.use(resResolve, resReject)
}
